<?php
include('init.php');
$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
switch($act)
{
	case 'transe':
	//update self account
	$loginAccountID = SessionUtil::getVar(SESSION_KEY_LOGIN_ID);
	$_REQUEST['CACHE_ACCOUNT_TABLE'][$loginAccountID][DBC_ACCOUNT_BALANCE] -= $_REQUEST['mount'];
	$db = AppMain::getDbDriver();
	$db->updateSetWhere(DB_TABLE_NAME_ACCOUNT,array(DBC_ACCOUNT_BALANCE => $_REQUEST['CACHE_ACCOUNT_TABLE'][$loginAccountID][DBC_ACCOUNT_BALANCE]), new SimpleWhereClause(DBC_ACCOUNT_ID, '=', $loginAccountID));
	//update others
	$toAccountID = $_REQUEST['toAccountID'];
	$_REQUEST['CACHE_ACCOUNT_TABLE'][$toAccountID][DBC_ACCOUNT_BALANCE] += $_REQUEST['mount'];
	$db->updateSetWhere(DB_TABLE_NAME_ACCOUNT,array(DBC_ACCOUNT_BALANCE => $_REQUEST['CACHE_ACCOUNT_TABLE'][$toAccountID][DBC_ACCOUNT_BALANCE]), new SimpleWhereClause(DBC_ACCOUNT_ID, '=', $toAccountID));
	$seperater1 = constant('SEPERATER_1');
	$seperater3 = constant('SEPERATER_3');
	$content = '';
	$content .= $seperater1.'<br>';
	$content .= sprintf("Transe %0.2f from [%s] to [%s]<br>",$_REQUEST['mount'],$_REQUEST['CACHE_ACCOUNT_TABLE'][$loginAccountID][DBC_ACCOUNT_NAME],$_REQUEST['CACHE_ACCOUNT_TABLE'][$toAccountID][DBC_ACCOUNT_NAME]);
	$content .= $seperater3.'<br>';
	global_load_account_info_to_request();
	$content .="Account Balance:<br>";
	foreach($_REQUEST['CACHE_ACCOUNT_TABLE'] as $tmpData)
	{
		$content .= $tmpData[DBC_ACCOUNT_NAME].':'.$tmpData[DBC_ACCOUNT_BALANCE].'<br>';
	}
	$content .= $seperater1.'<br>';
	
	
	$currentData = AppMain::getCurrentTime();
		$strTokenList = preg_split("/-/",$currentData);
		$logDir = constant('LOG_DATA_DIR').'/'.$strTokenList[0].'/'.$strTokenList[1].'/'.$strTokenList[2];
		if(!file_exists($logDir))
		{
			mkdir($logDir,0755,true);
		}
	$logFile = $logDir.'/'.time('His').'_'.Util::makeRandomStr(3).'.html';	
	file_put_contents($logFile,$content);
	//$headers= "From:$from\r\n";
	$headers = "Content-type:text/html;charset=utf-8" . "\r\n";
	$subject = 'Simple Finace System '.$currentData;
	mail($_REQUEST['CACHE_ACCOUNT_TABLE'][$loginAccountID][DBC_ACCOUNT_EMAIL], $subject, $content, $headers);
	mail($_REQUEST['CACHE_ACCOUNT_TABLE'][$toAccountID][DBC_ACCOUNT_EMAIL], $subject, $content, $headers);
	 header('Location:index.php');
	exit();
	break;
}
$account_list = $_REQUEST['CACHE_ACCOUNT_TABLE'];
?>
<?php include 'header.php' ?>
<?php include 'index_top.php' ?>

<script language="javascript" src="js/formvalidate.js"></script>
<script language="javascript">
function checkFormValue(form)
{
	var total = parseFloat($('input[name=mount]').val(),10);
	if(isNaN(total) || total == 0)
	{
		alert("请输入金额");
		return false;
	}
	form.submit();
	return false;
}
</script>
<form method="post">
<input type="hidden" name="act" value="transe"/>
<div class="adddiv">
	<label>账户</label>
	<select name="toAccountID">
		<?php foreach($account_list as $tmpData):?>
		<?php
			if($tmpData[DBC_ACCOUNT_ID] == SessionUtil::getVar(SESSION_KEY_LOGIN_ID))
				continue;
		?>
		<option value="<?php echo $tmpData[DBC_ACCOUNT_ID];?>"><?php echo $tmpData[DBC_ACCOUNT_NAME];?></option>
		<?php endforeach;?>
	</select>
	<label>金额</label>
	<input name="mount" class="_validate_form_float"/>
	<label></label>
	<input type="button" value="OK" class="button" onclick="return checkFormValue(this.form);"/>
</div>
</form>
<?php include 'footer.php' ?>